package cn.techjc;

import com.trilead.ssh2.Connection;
import com.trilead.ssh2.Session;
import com.trilead.ssh2.StreamGobbler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws IOException, InterruptedException {
	// write your code here
        Connection conn = new Connection("123.57.208.185",22);
        //root  6yjxkBvTEkys9f41
        conn.connect();
        boolean isOK = conn.authenticateWithPassword("root","6yjxkBvTEkys9f41");
        if(isOK){
            List<String> result = new ArrayList<String>();
            Session sess = conn.openSession();
            conn.requestRemotePortForwarding("0.0.0.0",6001,"127.0.0.1",80);
            conn.requestRemotePortForwarding("0.0.0.0",6002,"192.168.1.254",80);
            conn.requestRemotePortForwarding("0.0.0.0",6003,"192.168.1.254",80);
            conn.requestRemotePortForwarding("0.0.0.0",6004,"192.168.1.254",80);
            conn.requestRemotePortForwarding("0.0.0.0",6005,"192.168.1.254",80);
            conn.requestRemotePortForwarding("0.0.0.0",6006,"192.168.1.254",80);
            System.out.println("开启转发成功，等待600s");
            Thread.sleep(1000*600);

            sess.execCommand("netstat -antp");
            InputStream stdout = new StreamGobbler(sess.getStdout());
            BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
            while (true)
            {
                String line = br.readLine();
                if (line == null)
                    break;
                System.out.println(line);
                result.add(line);
            }
            sess.close();
            conn.close();
            System.out.println(result);
        }
    }
}
